//
//  ERequestAppRanking.m
//  AppRanking
//
//  Created by Gang.Wang on 12-11-8.
//  Copyright (c) 2012年 Gang.Wang. All rights reserved.
//

// @"http://www.ann9.com/hourxml12_11_488101334_0_0.json"

#import "ERequestAppRanking.h"
#import "mcf.h"

#define _GET_APP_RANKING_API_TEMPLATE_              @"http://%%country%%.ann9.com/hourxml%%category%%_%%devicemode%%%%paytype%%_%%appid%%_%%beforeday%%_%%beformonth%%.json"

#define _GET_APP_RANKING_API_COUNTRY_PARAM_         @"%%country%%"
#define _GET_APP_RANKING_API_CATEGORY_PARAM_        @"%%category%%"

#define _GET_APP_RANKING_API_DEVICE_PARAM_          @"%%devicemode%%"
#define _GET_APP_RANKING_API_PAYTYPE_PARAM_         @"%%paytype%%"

#define _GET_APP_RANKING_API_APPID_PARAM_           @"%%appid%%"
#define _GET_APP_RANKING_API_DAY_PARAM_             @"%%beforeday%%"

#define _GET_APP_RANKING_API_MONTH_PARAM_           @"%%beformonth%%"


@interface ERequestAppRanking (Private) 

- (NSString *) requestUrldownloadModel:(EAppInfo *) app country:(NSString *) country dayOrMonthValue:(int) dayOrMonthValue isDay:(BOOL) isDayOrMonth isiPhoneOs:(BOOL) isiPhoneOs;

- (YDownloadObjectModel *) downloadModel:(EAppInfo *) app country:(NSString *) country dayOrMonthValue:(int) dayOrMonthValue isDay:(BOOL) isDayOrMonth isiPhoneOs:(BOOL) isiPhoneOs;

@end


@implementation ERequestAppRanking

- (void) dealloc
{
    [super dealloc];
}



- (id) initWith:(EAppInfo *) app country:(NSString *) country day:(int) day isiPhoneOs:(BOOL) isiPhoneOs
{    
    self = [super init];
    if (self)
    {
        self.downloadModel = [self downloadModel:app country:country dayOrMonthValue:day isDay:YES isiPhoneOs:isiPhoneOs];
    }
    
    return self;
    
}

- (id) initWith:(EAppInfo *) app country:(NSString *)country month:(int)month isiPhoneOs:(BOOL) isiPhoneOs
{
    self = [super init];
    if (self)
    {
        self.downloadModel = [self downloadModel:app country:country dayOrMonthValue:month isDay:NO isiPhoneOs:isiPhoneOs];
    }
    
    return self;
}


#pragma mark - Private Method
- (NSString *) requestUrldownloadModel:(EAppInfo *) app country:(NSString *) country dayOrMonthValue:(int) dayOrMonthValue isDay:(BOOL) isDayOrMonth isiPhoneOs:(BOOL) isiPhoneOs
{
    NSString * result = _GET_APP_RANKING_API_TEMPLATE_;
    
    if ([NSString isNilOrEmpty:country])
    {
        result = [result replace:_GET_APP_RANKING_API_COUNTRY_PARAM_ with:@"www"];
    }
    else
    {
        result = [result replace:_GET_APP_RANKING_API_COUNTRY_PARAM_ with:country];
    }
    
    
    if (app.isFree)
    {
        result = [result replace:_GET_APP_RANKING_API_PAYTYPE_PARAM_ with:@"1"];
    }
    else
    {
        result = [result replace:_GET_APP_RANKING_API_PAYTYPE_PARAM_ with:@"2"];
    }
    
    
    if (isiPhoneOs)
    {
        result = [result replace:_GET_APP_RANKING_API_DEVICE_PARAM_ with:@"1"];
    }
    else
    {
        result = [result replace:_GET_APP_RANKING_API_DEVICE_PARAM_ with:@"2"];
    }
    
    
    if (isDayOrMonth)
    {
        result = [result replace:_GET_APP_RANKING_API_DAY_PARAM_ with:[NSString stringWithFormat:@"%d", dayOrMonthValue]];
        result = [result replace:_GET_APP_RANKING_API_MONTH_PARAM_ with:@"0"];
    }
    else
    {
        result = [result replace:_GET_APP_RANKING_API_MONTH_PARAM_ with:[NSString stringWithFormat:@"%d", dayOrMonthValue]];
        result = [result replace:_GET_APP_RANKING_API_DAY_PARAM_ with:@"0"];
    }
    
    result = [result replace:_GET_APP_RANKING_API_APPID_PARAM_ with:app.appId];
    result = [result replace:_GET_APP_RANKING_API_CATEGORY_PARAM_ with:[NSString stringWithFormat:@"%d", [self categoryNameToId:app.primaryGenreName]]];
    
    
    return result;
}

- (YDownloadObjectModel *) downloadModel:(EAppInfo *) app country:(NSString *) country dayOrMonthValue:(int) dayOrMonthValue isDay:(BOOL) isDayOrMonth isiPhoneOs:(BOOL) isiPhoneOs
{
    YDownloadObjectModel * downloadModel = [[[YDownloadObjectModel alloc] init] autorelease];
    
    downloadModel.url = [self requestUrldownloadModel:app country:country dayOrMonthValue:dayOrMonthValue isDay:isDayOrMonth isiPhoneOs:isiPhoneOs];
    
    [downloadModel setHttpMethod:@"GET"];
    
    NSMutableDictionary * httpHeaderValues = [[NSMutableDictionary alloc] init];
    
    NSString * userAgent = @"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11";    
    NSString * accept = @"application/json, text/javascript, */*; q=0.01";
    
    [httpHeaderValues setValue:userAgent                    forKey:@"User-Agent"];
    [httpHeaderValues setValue:@"en-US,en;q=0.8"            forKey:@"Accept-Language"];
    [httpHeaderValues setValue:@"gzip,deflate,sdch"         forKey:@"Accept-Encoding"];
    [httpHeaderValues setValue:@"GBK,utf-8;q=0.7,*;q=0.3"   forKey:@"Accept-Charset"];
    [httpHeaderValues setValue:accept                       forKey:@"Accept"];
    [httpHeaderValues setValue:@"keep-alive"                forKey:@"Connection"];    
    [httpHeaderValues setValue:@"XMLHttpRequest"            forKey:@"X-Requested-With"];
    [downloadModel setHttpHeaderField:httpHeaderValues];
    YRelease(httpHeaderValues)
    
    return downloadModel;
}



- (int) categoryNameToId:(NSString *) category
{
    int result = 33;
    if ([NSString isNilOrEmpty:category])
    {
        return result;
    }
    
    if ([category isEqualToString:@"Newsstand"])
    {
        result = 1;
        return result;
    }
    
    if ([category isEqualToString:@"Finance"])
    {
        result = 2;
        return result;
    }
    
    if ([category isEqualToString:@"Reference"])
    {
        result = 3;
        return result;
    }
    
    if ([category isEqualToString:@"Navigation"])
    {
        result = 4;
        return result;
    }
    
    if ([category isEqualToString:@"Utilties"])
    {
        result = 5;
        return result;
    }
    
    if ([category isEqualToString:@"Health & Fitness"])
    {
        result = 6;
        return result;
    }
    
    if ([category isEqualToString:@"Education"])
    {
        result = 7;
        return result;
    }
    
    if ([category isEqualToString:@"Travel"])
    {
        result = 8;
        return result;
    }
    
    if ([category isEqualToString:@"Food & Drink"])
    {
        result = 34;
        return result;
    }
    
    if ([category isEqualToString:@"Business"])
    {
        result = 9;
        return result;
    }
    
    if ([category isEqualToString:@"Catalogues"])
    {
        result = 34;
        return result;
    }
    
    if ([category isEqualToString:@"Social Networking"])
    {
        result = 10;
        return result;
    }
    
    if ([category isEqualToString:@"Photo & Video"])
    {
        result = 11;
        return result;
    }
    
    if ([category isEqualToString:@"Lifestyle"])
    {
        result = 12;
        return result;
    }
    
    if ([category isEqualToString:@"Sports"])
    {
        result = 13;
        return result;
    }
    
    if ([category isEqualToString:@"Weather"])
    {
        result = 14;
        return result;
    }
    
    if ([category isEqualToString:@"Books"])
    {
        result = 15;
        return result;
    }
    
    if ([category isEqualToString:@"Productivity"])
    {
        result = 16;
        return result;
    }
    
    if ([category isEqualToString:@"News"])
    {
        result = 17;
        return result;
    }
    
    if ([category isEqualToString:@"Medical"])
    {
        result = 18;
        return result;
    }
    
    if ([category isEqualToString:@"Music"])
    {
        result = 19;
        return result;
    }
    
    if ([category isEqualToString:@"Entertainment"])
    {
        result = 20;
        return result;
    }
    
    if ([category isEqualToString:@"Games"])
    {
        result = 21;
        return result;
    }
        
    return result;
}

@end



 
